package com.wb.listening

import com.wb.component.Slf4j.Companion.log
import com.wb.component.astrict.Astrict
import com.wb.component.event.MsgSend
import com.wb.component.event.OnGroup
import com.wb.component.event.OnPrivate
import com.wb.component.filter.FunFilter
import com.wb.component.filter.FunFilterType
import com.wb.component.filter.FunFilters
import com.wb.entity.MsgGet
import com.wb.utils.BiBiLiLiUtil
import com.wb.utils.DbbqbCom
import com.wb.utils.FileNetUtil
import com.wb.utils.SelectImage
import com.wb.utils.encryption.Md5Encryption
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.io.File
import java.util.*

/**
 * 通用消息监听
 *
 * @author 祁雪
 */
@Component
open class AllMsgListening {

    @Value("\${com.qbot.url}")
    lateinit var urlPriex: String

    @OnPrivate
    @OnGroup
    @FunFilters(
        value = arrayOf(
            FunFilter("表情包", FunFilterType.START_WITH),
            FunFilter("bqb", FunFilterType.START_WITH)
        )
    )
    open fun bqb(msg: MsgGet) {
        val text = msg.msg

        val searchKey = text.substring(3)

        if (searchKey.isEmpty()) {
            MsgSend.sendText(msg.from_wxid, "当前搜索词为空")
            return
        }
        try {
            val list = DbbqbCom.searchWeb(searchKey)
            if (list.size == 0) {
                MsgSend.sendText(msg.from_wxid, "暂未搜索到表情包")
                return
            }

            val url = list.get(
                (
                        if (list.size >= 10) {
                            Math.random() * 10
                        } else {
                            Math.random() * list.size
                        }
                        ).toInt()
            )
            // val fileName = url.substring(url.lastIndexOf("/") + 1)
            MsgSend.sendImage(
                msg.from_wxid,
                Md5Encryption.encode(url),
                url,
                ""
            )
            return
        } catch (e: Exception) {
            log.error("表情包搜索出现错误", e)
        }
        MsgSend.sendText(msg.from_wxid, "暂未搜索到表情包")
    }

    @OnPrivate
    @OnGroup
    @FunFilters(
        value = arrayOf(
            FunFilter("bz", FunFilterType.START_WITH),
            FunFilter("sjb", FunFilterType.START_WITH)
        )
    )
    @Astrict
    fun searchBL(msgGet: MsgGet) {
        val keys = arrayOf("bz", "sjb")
        var msg = msgGet.msg
        val text = msgGet.msg

        val key = keys.find { msg.startsWith(it) }
        msg = msg.substring(key!!.length)
        if (msg.isBlank()) {
            MsgSend.sendText(msgGet.from_wxid, "请在【bz/sjb】后面输入搜索内容")
            return
        }

        val list = BiBiLiLiUtil.search(msg, "")

        if (list.size == 0) {
            MsgSend.sendText(msgGet.from_wxid, "哔哩哔哩 => 暂未搜索到内容")
            return
        }

        val data = if (text.startsWith("sjb")) {
            list.get((Math.random() * list.size).toInt())
        } else {
            list.get(0)
        }

        val title = data.title.replace(Regex("<[^>]*>"), "")
        val pic = if (data.pic.startsWith("http")) data.pic else "https:${data.pic}"

        MsgSend.sendLink(
            msgGet.from_wxid,
            title,
            "UP主: ${data.author}",
            data.arcurl,
            pic,
            ""
        )

    }

    @OnGroup
    @OnPrivate
    @Astrict
    @FunFilter("来张涩图", FunFilterType.EQ)
    open fun girl(msg: MsgGet) {

        val files = SelectImage.getGirlPics(1)

        if (files.size == 0) {
            MsgSend.sendText(msg.from_wxid, "暂未获取到涩图")
            return
        }
        val file = files.get(
            (Math.random() * files.size).toInt()
        )

        MsgSend.sendImage(
            msg.from_wxid,
            file.name,
            "",
            file.absolutePath
        )

    }

    @OnPrivate
    @OnGroup
    @Astrict
    @FunFilter("来张小哥哥", FunFilterType.EQ)
    open fun boy(msg: MsgGet) {

        val files = SelectImage.getBoyPics(1)

        if (files.size == 0) {
            MsgSend.sendText(msg.from_wxid, "暂未获取到小哥哥")
            return
        }
        val file = files.get(
            (Math.random() * files.size).toInt()
        )

        MsgSend.sendImage(
            msg.from_wxid,
            file.name,
//            url,
            "",
            file.absolutePath
        )
    }

}